package com.ehotting.mqbd.core.service;

import com.ehotting.eaf.redis.service.RedisService;
import com.ehotting.mqbd.api.MQDBConstants;
import com.ehotting.mqbd.core.manager.AgentLinkManager;
import com.ehotting.mqbd.core.util.AgentIdGenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class AgentRefChannelService {

    private static Logger logger = LoggerFactory.getLogger(AgentRefChannelService.class);

    private TimeUnit TIME_UNIT = TimeUnit.DAYS;
    private Long TIME_OUT = 10L;

    @Autowired
    private RedisService redisService;



    public String getAgentIdByChannelId(String module,String channelId){
        String key = MQDBConstants.AGENT_REF_SOLVER_CHANNEL + module + MQDBConstants.COLO + channelId;
        String agentId =  redisService.getAndExpire(key, TIME_OUT,TIME_UNIT);
        logger.info("Get agentId[{}] by [module:{},channelId:{}] .",agentId,module,channelId);
        return agentId;
    }

    public void setAgentIdByChannelId(String module,String channelId,String agentId){
        logger.info("set module:{},channelId:{},agentId:{} .",module,channelId,agentId);
        String key = MQDBConstants.AGENT_REF_SOLVER_CHANNEL + module + MQDBConstants.COLO + channelId;
        redisService.set(key,agentId, TIME_OUT,TIME_UNIT);
    }

    public void deleteAgentIdByChannelId(String module,String channelId){
        logger.info("deleteByMsgId module{}, channelId:{}.",module,channelId);
        String key = MQDBConstants.AGENT_REF_SOLVER_CHANNEL + module + MQDBConstants.COLO + channelId;
        redisService.delete(key);
    }

    public String getClientIpByAgentId( String agentId){
        String key = MQDBConstants.AGENT_REF_CLIENT_IP + agentId;
        String clientIp = redisService.getAndExpire(key, TIME_OUT,TIME_UNIT);
        logger.info("Get clientIp[{}] by  agentId[{}].",clientIp,agentId);
        return clientIp;
    }




}
